import router from "@/router";
import $Vue from "@/main.js";
import { errorStatusCode } from "./util";
// 超时时间
axios.defaults.timeout = 5 * 1000; // 10s
//添加一个请求拦截器
axios.interceptors.request.use(
  function(config) {
    let token = sessionStorage.getItem("token");
    config.headers.common["Authorization"] = token;
    return config;
  },
  function(error) {
    // Do something with request error
    console.error("error: ");
    console.error(error);
    return Promise.reject(error);
  }
);
// 响应拦截器
axios.interceptors.response.use(
  (res) => {
    switch (res.data.statusCode) {
      case 401:
        sessionStorage.setItem("token", "");
        router.replace("/login");
        errorStatusCode(res.data.statusCode, res.data);
        break;
    }
    return res;
  },
  function(err) {
    $Vue.$Spin.hide();
    //错误处理
    console.error(err);
  }
);
